/**
 * 用户TOKEN方法封装
 * Copyright (c) 2019 itsoft
 */
import Cookies from 'js-cookie'
import Config from '../config/index'
import {
    Encrypt,
    Decrypt
} from '../utils/dencrypt'
import request from "@/itsoftUI/utils/request";
import store from '../../store'
//失效时间20分钟
//const  expiresTime = new Date(new Date().getTime() + 20 * 60 * 1000);
export default {
    /**
     * 生产token
     */
    getToken() {
        let tokenBody = {}
        let strTokenKey = Cookies.get(Config.cookieKey)
        // clientId:'web201911chengdu',
        if (strTokenKey) {
            tokenBody.tokenKey = strTokenKey
            tokenBody.clientId = Config.clientId
            tokenBody.timestamp = (new Date()).valueOf()
            tokenBody.GUID = this.guid()
            let strTokenBody = JSON.stringify(tokenBody)
            //加密
            strTokenBody = Encrypt(strTokenBody, Config.clientSecret, Config.clientId)
            return strTokenBody
        }
        return ""
    },
    /**
     * 从cookie 获取 tokenKey
     */
    getTokenKey() {
        let strTokenKey = Cookies.get(Config.cookieKey)

        return strTokenKey
    },
    /**
     * 保存tokenKey
     */
    saveTokenKey(tokenKey) {
        //加密存储
        var inFifteenMinutes = new Date(new Date().getTime() + 24 * 60 * 60 * 1000)
        return Cookies.set(Config.cookieKey, tokenKey, {
            expires: inFifteenMinutes
        })
    },
    /**
     * 是否登陆
     */
    isLogin() {
        // return (!!Cookies.get(Config.cookieKey) && Cookies.get(Config.cookieKey) != '0')
        return Cookies.get(Config.cookieKey)

    },
    /**
     * 获取用户信息
     */
    async getUserInfo() {
        let userInfo = localStorage.getItem("USER_INFO")
        if (userInfo) {
            userInfo = JSON.parse(Decrypt(userInfo));
            store.state.userInfo = userInfo
        } else {
            await request({
                url: Config.serviceUrl + '/auth/user/info',
                method: 'get'
            }).then(res => {
                localStorage.setItem("USER_INFO", Encrypt(JSON.stringify(res.data)))
                userInfo = res.data
            })
        }
        return userInfo
    },
    async isForbidden() {
        let flag = ""
        await request({
            url: Config.serviceUrl + '/supervise/supervisionPost/isForbidden',
            method: 'post',
        }).then(res => {

            flag = res.data
        });
        return flag
    },
    /**
     * 退出
     */
    logout() {
        //退出
        request({
            url: Config.serviceUrl + '/auth/user/logout',
            method: 'get'
        }).then(
            res => {
                this.localLogout()
                // debugger
                if (Config.casUrl) {
                    // window.location.href = Config.casUrl + "/logout?redirect=" + window.location.href
                    window.location.href = Config.casLogoutUrl
                } else {
                    window.location.reload()
                }
                return res

            }
        );
    },
    localLogout() {
        localStorage.removeItem("USER_INFO")
        Cookies.remove(Config.cookieKey)

    },
    getTimestamp() {
        return Encrypt(new Date().valueOf())
    },
    guid() {
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            let r = Math.random() * 16 | 0,
                v = c === 'x' ? r : (r & 0x3 | 0x8);
            return v.toString(16);
        });
    }


}